﻿using System;
using System.Collections.Generic;
using System.Drawing;


namespace FXnaGis
{
    /// <summary>
    /// DuplicationPreventer
    /// 主要是用来防止浮点数转化为 整数时 ，产生的相等值
    /// </summary>
    public static class DuplicationPreventer
    {
        const int X = 0;
        const int Y = 1;

        #region Methods


        //public static List<Point> Clean(List<Point> points)
        //{
        //    List<Point> result = new List<Point>();
        //    Point previous = Point.Empty;
        //    bool isFirst = true;

        //    foreach (Point point in points)
        //    {
        //        Point pt = new Point();
        //        pt.X = Convert.ToInt32(point.X);
        //        pt.Y = Convert.ToInt32(point.Y);
        //        if (isFirst || pt.X != previous.X || pt.Y != previous.Y)
        //        {
        //            isFirst = false;
        //            previous = pt;
        //            result.Add(pt);
        //        }
        //    }
        //    return result;
        //}



        /// <summary>
        /// Cycles through the PointF points, where necessary and removes duplicate points
        /// that are found at the integer level.
        /// </summary>
        /// <param name="points"></param>
        /// <returns></returns>
        public static List<Point> Clean(List<PointF> points)
        {
            List<Point> result = new List<Point>();
            Point previous = Point.Empty;
            bool isFirst = true;
            foreach (PointF point in points)
            {
                Point pt = new Point();
                pt.X = Convert.ToInt32(point.X);
                pt.Y = Convert.ToInt32(point.Y);
                if (isFirst || pt.X != previous.X || pt.Y != previous.Y)
                {
                    isFirst = false;
                    previous = pt;
                    result.Add(pt);
                }
            }
            return result;
        }

        /// <summary>
        /// Cleans the list of points by removing duplicates
        /// </summary>
        /// <param name="points"></param>
        /// <returns></returns>
        public static List<Point> Clean(List<double[]> points)
        {
            List<Point> result = new List<Point>();
            Point previous = Point.Empty;
            bool isFirst = true;
            foreach (double[] point in points)
            {
                if (double.IsNaN(point[X]) || double.IsNaN(point[Y])) continue;
                Point pt = new Point();
                pt.X = Convert.ToInt32(point[X]);
                pt.Y = Convert.ToInt32(point[Y]);
                if (isFirst || pt.X != previous.X || pt.Y != previous.Y)
                {
                    isFirst = false;
                    previous = pt;
                    result.Add(pt);
                }
            }
            return result;
        }

        #endregion
    }

}
